package com.qingfeng.client.api;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.qingfeng.base.exception.LsException;
import com.qingfeng.base.model.Page;
import com.qingfeng.base.util.CodeEnum;
import com.qingfeng.client.bean.Seller;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangyuefei
 * @version 1.0
 * @date 2020/1/2 4:19 下午
 */
public class BaseV1Api {
//
//    @Autowired
//    protected HttpServletRequest request;

    public Seller getUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication.getPrincipal() instanceof String) {
            throw new LsException(CodeEnum.UNAUTHORIZED.getCode(), "当前用户未登录");
        }
        Seller tUser = (Seller) authentication.getPrincipal();
        return tUser;
    }

//    public ApiMsg returnFlag(boolean flag){
//        if (flag){
//            return success();
//        }else {
//            return error();
//        }
//    }
//
//    public ApiMsg returnFlag(int flag){
//        if (flag > 0){
//            return success();
//        }else {
//            return error();
//        }
//    }
//
//    public ApiMsg error(){
//        return new ApiMsg(500,"失败",null);
//    }
//
//    public ApiMsg error(String msg){
//        return new ApiMsg(500,msg,null);
//    }
//
//    public ApiMsg success(){
//        return new ApiMsg(200,"成功",null);
//    }
//
//    public ApiMsg success(String msg){
//        return new ApiMsg(200,msg,null);
//    }
//    public ApiMsg success(String msg, Object data){
//        return new ApiMsg(200,msg,data);
//    }
//
//    public ApiMsg success(Object data){
//        return new ApiMsg(200,"成功",data);
//    }

    public JSONObject successPage(IPage page) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", page.getRecords());
        page.setRecords(null);
        jsonObject.put("page", page);
        jsonObject.put("result", 1);
        return jsonObject;
    }

    public JSONObject success(Object object) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", 1);
        jsonObject.put("object", object);
        return jsonObject;
    }

    public JSONObject success(String msg) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", 1);
        jsonObject.put("msg", msg);
        return jsonObject;
    }

    public JSONObject error(String msg) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", 0);
        if (!StringUtils.isEmpty(msg)) {
            jsonObject.put("msg", msg);
        }
        return jsonObject;
    }

    public JSONObject returnBoolean(boolean flag) {
        JSONObject jsonObject = new JSONObject();
        if (flag) {
            jsonObject.put("result", 1);
        } else {
            jsonObject.put("result", 0);
        }
        return jsonObject;
    }
    protected Page parsePageJSON(JSONObject pageJson) {
        Page page = new Page();
        page.setPageNum(pageJson.getInteger("pageNum"));
        page.setPageSize(pageJson.getInteger("pageSize"));
        String sortOrder = pageJson.getString("sortorder");
        String sortName = pageJson.getString("sortname");
        if (!org.springframework.util.StringUtils.isEmpty(sortName)) {
            List<OrderItem> orders = new ArrayList<>();
            OrderItem orderItem = new OrderItem();
            if (org.springframework.util.StringUtils.isEmpty(sortOrder) || sortOrder.equals("desc")) {
                orderItem.setAsc(false);
            } else {
                orderItem.setAsc(true);
            }
            orderItem.setColumn(sortName);
            orders.add(orderItem);
            page.setOrders(orders);
        }
        return page;
    }
}
